package cn.lf.android.lawfirm.home.controller;

import android.content.Context;
import android.util.Log;

import com.alibaba.fastjson.JSON;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.lf.android.lawfirm.AppConfig;
import cn.lf.android.lawfirm.BaseController;
import cn.lf.android.lawfirm.home.modle.UserInfo;
import cn.lf.android.lawfirm.http.manager.EBCallback;
import cn.lf.android.lawfirm.http.manager.MyOkHttpClient;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.SPUtil;


public class UserInfoController extends BaseController {
    UserInfoCallback mUserInfoCallback;
    private Context context;

    public UserInfoController(UserInfoCallback callback, Context context) {
        this.mUserInfoCallback = callback;
        this.context = context;
    }

    public void onUserInfo() {
        DebugLog.e("77==" + "123654987===================");
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("Authorization", spUtil.getAccessToken());
        DebugLog.e("77==" + spUtil.getAccessToken());
        new MyOkHttpClient().doGet(AppConfig.LsysUsers_UserBasicInfo, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("个人信息=66==" + response);
                        JSONObject myJsonObject;
                        JSONObject responsObject;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                responsObject = myJsonObject.getJSONObject("responsObject");
                                UserInfo bean = JSON.parseObject(responsObject.toString(), UserInfo.class);
                                SPUtil sp = new SPUtil(context);
                                sp.setUserInfo(bean);
                                mUserInfoCallback.onUserInfoSuccessed(bean);
                            } else {
                                mUserInfoCallback
                                        .onUserInfoFailed();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        Log.e("个人信息失败=66==", errorMsg);
                        mUserInfoCallback.onUserInfoFailed();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }

    /**
     * 首页回调接口
     */
    public interface UserInfoCallback {
        /**
         * 首页请求成功
         */
        public void onUserInfoSuccessed(UserInfo beans);

        /**
         * 首页请求失败
         */
        public void onUserInfoFailed();

    }
}
